﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace UnityGameFramework.Editor
{
    /// <summary>
    /// 类型相关的实用函数。
    /// </summary>
    public static class Type
    {
        private static readonly string[] RuntimeAssemblyNames =
        {
#if UNITY_2017_3_OR_NEWER
            "UnityGameFramework.Runtime",
#endif
            "Assembly-CSharp",
        };

        private static readonly string[] EditorAssemblyNames =
        {
#if UNITY_2017_3_OR_NEWER
            "UnityGameFramework.Editor",
#endif
            "Assembly-CSharp-Editor",
        };

        private static TypeSearch s_TypeSearch = new TypeSearch();

        /// <summary>
        /// 获取配置路径。
        /// </summary>
        /// <typeparam name="T">配置类型。</typeparam>
        /// <returns>配置路径。</returns>
        public static string GetConfigurationPath<T>() where T : ConfigPathAttribute
        {
            foreach (System.Type type in Utility.Assembly.GetTypes())
            {
                if (!type.IsAbstract || !type.IsSealed)
                {
                    continue;
                }

                foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
                {
                    if (fieldInfo.FieldType == typeof(string) && fieldInfo.IsDefined(typeof(T), false))
                    {
                        return (string)fieldInfo.GetValue(null);
                    }
                }

                foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
                {
                    if (propertyInfo.PropertyType == typeof(string) && propertyInfo.IsDefined(typeof(T), false))
                    {
                        return (string)propertyInfo.GetValue(null, null);
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// 在运行时程序集中获取指定基类的所有子类的名称。
        /// </summary>
        /// <param name="typeBase">基类类型。</param>
        /// <returns>指定基类的所有子类的名称。</returns>
        public static string[] GetRuntimeTypeNames(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(RuntimeAssemblyNames);
            var setting = GameFrameworkSettings.SettingObject;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyName(setting.RuntimeSearchedScopeAssemblyNames.ToArray());
                s_TypeSearch.AddAssemblyDefinition(setting.RuntimeSearchedScopeDefinitions.ToArray());
            }
            return GetTypeNames(s_TypeSearch, typeBase);
        }

        /// <summary>
        /// 在运行时或编辑器程序集中获取指定基类的所有子类的名称。
        /// </summary>
        /// <param name="typeBase">基类类型。</param>
        /// <returns>指定基类的所有子类的名称。</returns>
        public static string[] GetRuntimeOrEditorTypeNames(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(RuntimeAssemblyNames);
            s_TypeSearch.AddAssemblyName(EditorAssemblyNames);
            var setting = GameFrameworkSettings.SettingObject;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyName(setting.RuntimeSearchedScopeAssemblyNames.ToArray());
                s_TypeSearch.AddAssemblyDefinition(GameFrameworkSettings.SettingObject.RuntimeSearchedScopeDefinitions.ToArray());
                s_TypeSearch.AddAssemblyName(setting.EditorSearchedScopeAssemblyNames.ToArray());
                s_TypeSearch.AddAssemblyDefinition(GameFrameworkSettings.SettingObject.EditorSearchedScopeDefinitions.ToArray());
            }
            return GetTypeNames(s_TypeSearch, typeBase);
        }

        private static string[] GetTypeNames(TypeSearch typeSearch,System.Type typeBase)
        {
            List<string> typeNames = 
                typeSearch
                .Search((t) => t.IsClass && !t.IsAbstract && typeBase.IsAssignableFrom(t))
                .Select(t=>t.FullName)
                .ToList();
            typeNames.Sort();
            return typeNames.ToArray();
        }
    }
}
